<?php

include ('App.php');
include ('Collection.php');

class UserClass {
	//  Region:  DB / DB Fields
	const __TBL = "this_is_a_test";
	const __FLD_ID = "ID";
	const __FLD_USER_ID = "User_ID";
	const __FLD_USER_NAME = "User_Name";
	const __FLD_USER_ONE = "User_One";
	const __FLD_USER_TWO = "User_two";
	const __FLD_USER_THREE = "User_three";
	const __FLD_USER_FOUR = "User_four";
	private $conn;
	//  Region:  DB / DB Fields End

	//  Region:  Properties
	private $Id;
	private $UserId;
	private $UserName;
	private $UserOne;
	private $UserTwo;
	private $UserThree;
	private $UserFour;
	private $isLoaded;

	function __construct($Id = "",$UserId = "",$UserName = "",$UserOne = "",$UserTwo = "",$UserThree = "",$UserFour = "") {
		$this->conn 		=		App::getDB();
		$this->Id			=		$Id;
		$this->UserId		=		$UserId;
		$this->UserName 	=		$UserName;
		$this->UserOne		=		$UserOne;
		$this->UserTwo		=		$UserTwo;
		$this->UserThree	=		$UserThree;
		$this->UserFour		=		$UserFour;
		
	}

	public function returnTable(){
		return 	self::__TBL;
	}

// Region: Standard Functions
	public function populate($PrimKeyValue){
		App::$DAL->SqlBuilder->Table(self::__TBL);
		App::$DAL->SqlBuilder->Where(self::__FLD_ID,"=",$prinKeyValue);
		$sql = App::$DAL->SqlBuilder->SELECTStatement();
		$row = App::$DAL->GetDataRow($sql);
		$this->getRow($row);
	}
	private function getRow($row){
		$this->Id = $row[self::__FLD_ID];
		$this->UserId = $row[self::__FLD_USER_ID];
		$this->UserName = $row[self::__FLD_USER_NAME];
		$this->UserOne = $row[self::__FLD_USER_ONE];
		$this->UserTwo = $row[self::__FLD_USER_TWO];
		$this->UserThree = $row[self::__FLD_USER_THREE];
		$this->UserFour = $row[self::__FLD_USER_FOUR];
		$this->isLoaded = true;
	}

	public function save() {
		App::$DAL->SqlBuilder->Table(self::__TBL);
		App::$DAL->SqlBuilder->Field(self::__FLD_ID,$this->Id);
		App::$DAL->SqlBuilder->Field(self::__FLD_USER_ID,$this->UserId);
		App::$DAL->SqlBuilder->Field(self::__FLD_USER_NAME,$this->UserName);
		App::$DAL->SqlBuilder->Field(self::__FLD_USER_ONE,$this->UserOne);
		App::$DAL->SqlBuilder->Field(self::__FLD_USER_TWO,$this->UserTwo);
		App::$DAL->SqlBuilder->Field(self::__FLD_USER_THREE,$this->UserThree);
		App::$DAL->SqlBuilder->Field(self::__FLD_USER_FOUR,$this->UserFour);
		if ($this->isLoaded === true) {
			App::$DAL->SqlBuilder->Where(self::__FLD_ID,"=",$this->Id);
			$SQL = App::$DAL->SqlBuilder->UPDATEStatement();
		} else {
			$SQL = App::$DAL->SqlBuilder->INSERTStatement();
		}
		App::$DAL->Execute($SQL);
	}
}

class UserClassCollection{

	private $Class;
	private $__TBL;
	public $Collection;
	
	function __construct() {
		$this->Collection 			= 	new Collection;
		$this->Class				= 	new UserClass;
		$this->__TBL				= 	$this->Class->returnTable();
	}
	
	public  function __toString(){
		return $this->Class->UserName;
	}
	
	public function Retreve(){
		try{
			App::$DAL->SqlBuilder->Table($this->__TBL);
			$sql = App::$DAL->SqlBuilder->SELECTStatement();
			$DataTable = App::$DAL->getArrayFromDB($sql);
			foreach ($DataTable as $row) {
				$this->Collection->addItem(
					new UserClass(
						$row['ID'],
						$row['User_ID'],
						$row['User_Name'],
						$row['User_One'],
						$row['User_two'],
						$row['User_three'],
						$row['User_four']
					),
					$row['ID']
				);
			}
		}catch (Exception $e){
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
	}

	public function getItemByKey($Key){
		try{
			return $this->Collection->getItem($Key);
		}catch (Exception $e){
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
	}
	
	public function Count(){
		try{
			return $this->Collection->length();
		}catch (Exception $e){
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
	}
	
	public function RemoveItemByKey($Key){
		try{
			return $this->Collection->removeItem($key);
		}catch (Exception  $e){
			App::$Logger->Log(LogType::Error,$e->getMessage());
		}
	}
}
?>